package cn.yunyichina.provider.framework.dubbo.core.filter;

import cn.yunyichina.provider.framework.dubbo.core.context.TraceContext;
import cn.yunyichina.utils.commons.IDUtils;
import com.alibaba.dubbo.rpc.*;

import java.util.Map;

/**
 * Created by kevin on 2017/11/30.
 */
public class TraceProviderFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Map<String, String> attaches = invocation.getAttachments();
        if (attaches.containsKey(TraceContext.TRACE_ID_KEY)){
            String traceId = attaches.get(TraceContext.TRACE_ID_KEY);
            TraceContext.clear();
            TraceContext.setTraceId(traceId);
        }else{
            TraceContext.clear();
            String id= IDUtils.getRandomUUID32();
            TraceContext.setTraceId(id);
        }
        return invoker.invoke(invocation);
    }
}
